//
//  PCDApiNavigateBack.m
//  PCDBank
//
//  Created by DK on 2021/2/25.
//  Copyright © 2021 DK. All rights reserved.
//

#import "PCDApiNavigateBack.h"
#import "PCDDataBaseModel.h"
#import "PCDBankWebViewController.h"

@implementation PCDApiNavigateBack

- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback )responseCallback{

    NSDictionary *tempDic = (NSDictionary *)data;
    NSString *callBack = [tempDic objectForKey:@"success"];
    NSString *appId = [tempDic objectForKey:@"appId"];
    NSInteger delta = [[tempDic objectForKey:@"delta"] intValue];
    
    //默认1
    if (delta == 0) {
        delta = 1;
    }
    
    NSString *extraData =[tempDic objectForKey:@"extraData"];
    [PCDDataBaseModel shareModel].extraData = extraData;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if([PCDPageViewMangeGet().appId isEqual:appId])
        {
            NSMutableArray *viewCtrs = [NSMutableArray arrayWithArray:PCDPageViewMangeGet().baseNavgatView.viewControllers];
            NSInteger index = viewCtrs.count- delta-1;
            UIViewController *view = [viewCtrs objectAtIndex:index];
            [self clearViewsOfDelta:delta];
            [PCDPageViewMangeGet().baseNavgatView popToViewController:view animated:YES];
            
        }else if(![PCDPageViewMangeGet().appId isEqual:appId] &&  ![PCDUtil nilOrEmptyString:appId]){
            
            NSMutableArray *viewCtrs = [self clearViews];
            [PCDPageViewMangeGet().baseNavgatView setViewControllers:viewCtrs animated:YES];

        }else if([PCDUtil nilOrEmptyString:appId]){
            //没有appid ，根据delta数退栈
            NSMutableArray *viewCtrs = [NSMutableArray arrayWithArray:PCDPageViewMangeGet().baseNavgatView.viewControllers];
            
            if(viewCtrs.count == 0){
                
                UIViewController * viewController = context.viewController;
                [viewController.navigationController popViewControllerAnimated:YES];
                
                return;
            }
            
            NSInteger index = viewCtrs.count -delta;
            NSRange range = NSMakeRange(index, delta);
            if(delta > viewCtrs.count){
                range = NSMakeRange(0, viewCtrs.count);
            }
            [viewCtrs removeObjectsInRange:range];
            
            [self clearViewsOfDelta:delta];
            
            if(viewCtrs.count == 0){
                UIViewController * viewController = context.viewController;
                [viewController.navigationController popToRootViewControllerAnimated:YES];
            }else{
                [PCDPageViewMangeGet().baseNavgatView setViewControllers:viewCtrs animated:YES];
            }
            
            
//            id tampView = [viewCtrs lastObject];
//            if ([tampView isKindOfClass:[PCDBankWebViewController class]]) {
//
//                if (PCDPageViewMangeGet().baseUrlList.count > delta) {
//                    NSInteger count = PCDPageViewMangeGet().baseUrlList.count - delta;
//                    NSMutableArray *array = [NSMutableArray array];
//                    for (NSInteger i = 0 ; i < count; i ++) {
//                        [array addObject:PCDPageViewMangeGet().baseUrlList[i]];
//                    }
//                    PCDPageViewMangeGet().baseUrlList = array;
//                } else {
//                    if (PCDPageViewMangeGet().baseUrlList.count >0) {
//                        [PCDPageViewMangeGet().baseUrlList removeLastObject];
//                    }
//                }
//
//                PCDBankWebViewController *bankView = (PCDBankWebViewController *)tampView;
//                if (![PCDPageViewMangeGet().appId isEqual:bankView.appId]) {
//
//                    PCDPageViewMangeGet().appId = bankView.appId;
//                    PCDPageViewMangeGet().baseUrl = [PCDPageViewMangeGet().baseUrlList lastObject];
//                }
//                [PCDPageViewMangeGet().baseNavgatView setViewControllers:viewCtrs animated:YES];
//
//            }else{
//
//                if(viewCtrs.count == 0){
//                    UIViewController * viewController = context.viewController;
//                    [viewController.navigationController popToRootViewControllerAnimated:YES];
//                    return;
//                }
//                [PCDPageViewMangeGet().baseNavgatView setViewControllers:viewCtrs animated:YES];
//            }
            
        }else{
            
            [self clearViewsOfDelta:1];
            [PCDPageViewMangeGet().baseNavgatView popViewControllerAnimated:YES];
        }
        
        if (![PCDUtil nilOrEmptyString:callBack]) {
            //
            evaluateJavaScriptOnMainThread_quotes(self,callBack,@"");
        }
    });
    
    
}

-(NSMutableArray *)clearViews
{
    NSMutableArray *viewCtrs = [NSMutableArray arrayWithArray:PCDPageViewMangeGet().baseNavgatView.viewControllers];
    
    NSInteger i =viewCtrs.count-1;
    while (i>=0) {
        
        id view = [viewCtrs objectAtIndex:i];
        
        if ([view isKindOfClass:[PCDBankWebViewController class]]) {
            
            PCDBankWebViewController *viewPage = (PCDBankWebViewController *)view;
            NSString *strAppId = viewPage.appId;
            
            
            if ([PCDPageViewMangeGet().appId isEqual:strAppId]) {
                [viewCtrs removeObjectAtIndex:i];
                
                if (PCDPageViewMangeGet().baseUrlList.count >0) {
                    [PCDPageViewMangeGet().baseUrlList removeLastObject];
                }else{
                    PCDPageViewMangeGet().appId = 0;
                    PCDPageViewMangeGet().baseUrl = nil;
                }
                

            }else{
                
                break;
            }
            i--;
        }else{
            break;
        }
    }
    return viewCtrs;
}


-(void)clearViewsOfDelta:(NSInteger)delta
{
    int count = PCDPageViewMangeGet().baseUrlList.count;
    int index   = count-delta;
    if(index>0)
    {
        NSRange range = NSMakeRange(index, delta);
        [PCDPageViewMangeGet().baseUrlList removeObjectsInRange:range];
    }

}

@end
